<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Member as RepositoriesMember;
use App\Admin\Repositories\MemberDoctor;
use App\Models\Member;
use App\Models\MemberDoctor as AppMemberDoctor;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class MemberDoctorController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new MemberDoctor(['member']), function (Grid $grid) {
            $grid->disableCreateButton();
            $titles = ['id' => 'ID', 'real_name' => '真实姓名', 'unit' => '工作单位', 'title' => '职称', 'department' => '科室', 'phone' => '手机号码', 'bank_number' => '银行卡号',  'opening_bank' => '开户支行'];
            $grid->export($titles);
            // $grid->disableViewButton();
            $grid->column('id')->sortable();
            $grid->column('user_id', '用户名称')->display(function ($id) {
                return Member::where('userid', $id)->first()->username ??  '暂无';
            })->label();
            $grid->column('real_name', '真实姓名')->copyable();
            $grid->column('unit', '工作单位');
            $grid->column('department', '科室');
            $grid->column('title', '职称');
            $grid->column('unit', '工作单位');
            $grid->column('status', '认证状态')->display(function ($status) {
                $state = ['未认证', '已申请', '已认证', '驳回'];
                return $state[$status];
            })->label();
            $grid->column('created_at');
            $grid->model()->orderBy('id', 'desc');
            /**
             * 按钮控制.
             */
            // @permissions
            if (!Admin::user()->can('user-cert-create')) {
                $grid->disableCreateButton();
                // $grid->disableQuickCreateButton();
            }
            // @permissions
            if (!Admin::user()->can('user-cert-update')) {
                $grid->disableEditButton();
                // $grid->disableQuickEditButton();
            }
            // @permissions
            if (!Admin::user()->can('user-cert-delete')) {
                $grid->disableDeleteButton();
            }
            $grid->filter(function (Grid\Filter $filter) {
                // $filter->equal('id');
                $filter->equal('member.username', '用户名或者手机号');
                $filter->like('real_name', '姓名');
            });
            $grid->selector(function (Grid\Tools\Selector $selector) {

                $selector->select('status', '状态', ['未认证', '已申请', '已认证', '驳回']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new MemberDoctor(), function (Show $show) {

            // @permissions
            if (!Admin::user()->can('user-cert-delete')) {
                $show->disableDeleteButton();
            }

            // @permissions
            if (!Admin::user()->can('user-cert-update') || (Admin::user()->id != $show->model()->admin_id) && !Admin::user()->isAdministrator()) {
                $show->disableEditButton();
            }

            $show->field('id');
            $show->field('user_id');
            $show->field('real_name', '真实姓名');
            $show->field('unit', '工作单位');
            $show->field('department', '科室');
            $show->field('title', '职称');
            $show->field('unit', '工作单位');
            $show->field('phone', '手机号码');
            $show->field('bank_number', '银行卡号');
            $show->field('opening_bank', '支行');
            $show->certificate('工牌照片')->image();
            $show->field('reason', '审核意见');
            $show->field('examine_time', '审核时间');
            $show->field('created_at');
            $show->field('updated_at');
            $show->field('sort');

            $show->tools(function (Show\Tools $tools) {
                $tools->append('<a class="btn btn-sm btn-outline-success  " style="margin-right:5px" href="javascript:history.back(-1)">返回</a>');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new MemberDoctor(), function (Form $form) {
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->disableViewButton();
            // @permissions
            if (!Admin::user()->can('user-cert-delete')) {
                $form->disableDeleteButton();
            }
            $form->display('id');
            // $form->text('user_id')->customFormat(function ($id) {
            //     return AppMemberDoctor::where('user_id', $id)->first()->real_name ??  '暂无';
            // })->disable();
            $form->text('real_name', '真实姓名')->disable();
            $form->text('unit', '单位')->disable();
            $form->text('department', '科室')->disable();
            $form->text('title', '职称')->disable();
            $form->text('phone', '手机号')->disable();
            $form->text('id_card_number', '身份证号码')->disable();
            $form->text('bank_number', '银行卡号')->disable();;
            $form->text('opening_bank', '支行')->disable();;
            $form->image('certificate', '工牌照片')->disable();
            // @permissions
            if (Admin::user()->can('user-cert-examine')) {
                $form->radio('status', '状态')->when([3], function (Form $form) {
                    $form->textarea('reason', '意见');

                    $form->hidden('examine_time');
                    $form->saving(function (Form $form) {
                        $form->examine_time = date('Y-m-d H:i:s', time());
                    });
                })->options(['未认证', '已申请', '已认证', '驳回'])->default(1);
            }

            $form->tools(function (Form\Tools $tools) {
                $tools->append('<a class="btn btn-sm btn-outline-success  " style="margin-right:5px" href="javascript:history.back(-1)">返回</a>');
            });

            $form->saved(function (Form $form) {
                $user_id = $form->model()->user_id;
                $member = Member::find($user_id);
                $member->realname = $form->model()->real_name;
                $member->doctor_status = $form->input('status');
                $member->save();
            });
        });
    }
}
